package com.bilubi.reggie.reggie.controller;

import com.bilubi.reggie.reggie.bean.AddressBook;
import com.bilubi.reggie.reggie.bean.User;
import com.bilubi.reggie.reggie.common.R;
import com.bilubi.reggie.reggie.service.AddressBookService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public R<List<AddressBook>> getAddressBookByUserId(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Long userId = user.getId();
        List<AddressBook> addressBooks = addressBookService.selectAddressBookByUserId(userId);
        return R.success(addressBooks);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public R<AddressBook> getAddressBookById(@PathVariable(value = "id") Long id) {
        AddressBook addressBook = addressBookService.selectAddressBookById(id);
        return R.success(addressBook);
    }

    @RequestMapping(method = RequestMethod.PUT)
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.updateAddressBook(addressBook);
        return R.success();
    }

    @RequestMapping(method = RequestMethod.POST)
    public R<String> addAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.addAddressBook(addressBook);
        return R.success();
    }

    @RequestMapping(value = "/default", method = RequestMethod.PUT)
    public R<String> updateDefaultAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.updateDefaultAddressBook(addressBook);
        return R.success();
    }

    @RequestMapping(value = "/default", method = RequestMethod.GET)
    public R<AddressBook> getDefaultAddressBook() {
        AddressBook addressBook = addressBookService.getDefaultAddressBook();
        return R.success(addressBook);
    }

}
